import { privacyChannel } from "./privacyChannel";
import { privacyChannelWeb } from "./privacyChannelWeb";


/**立方体大师3d  mgvgeoflzhsf0gfjz*/
export class pwrChannelMg extends privacyChannelWeb {

    channel: string = 'mg';

    paraMap: Map<String, string> = new Map([
        ['appId', 'mgvgeoflzhsf0gfjz'],
        //['bannerAd', '12260c3c019945c9b667f6299fdc3949'],
        // ['nativeBannerAd', '03154b46e03b49cb9d496d207264e7f1'],
        // ['insertAd', '807f7131e4294e40a3d62092a9c4eadb'],
        // ['customInsertAd', 'b5e3b88b0d86446ea77478ef2862833e'],
        ['videoId', 'm9nfrlva7pveoatk'],
        // ['gird9', 'fbe490cba3e5444a962fc37e773562e4'],
        //['ctrlId', '67cfded2c8946b1700d95e9d'], //策略开头id
        // ['gid', '674546f9c5b6582182fb7e6c'], //gid
    ]);
    company: string = '南京桔柚檬网络科技有限公司';


    ruanzhu: string = '著作权人:南京桔柚檬网络科技有限公司\n登记号:2021SR0614117';


    // @ts-ignore
    channelObj: any = (window as any).mgtv;

    // /* 是否同意了隐私政策 */
    // hasPrivacy(): boolean {
    //     if(cc.sys.)
    //     return !!pwrkc.getData('isAgree', 0);
    // }

    /** getSystemInfoSync */
    getSysInfo(): any {
        if (!this.sysInfo) {
            this.sysInfo = this.channelObj.getSystemInfoSync();
        }
        return this.sysInfo;
    }

    exit(): void {
        this.channelObj.exitApplication({ data: 0 });
    }


    init() {
        //super.init(loginCall);
        // sfrkc.log('隐私', this.hasPrivacy());
        const ctrlId = this.getPara('ctrlId');
        if (ctrlId) {
            const reqData = { 'id': ctrlId, 'fromid': 1 };
            privacyChannel.jpost('https://ga.gametdd.com/adCtrl/agd/conf', reqData, (err, re) => {
                if (err) {
                    console.error(err);
                    return;
                }
                if ('' + re.code !== '0') {
                    console.error('返回值错误:' + JSON.stringify(re));
                    return;
                }
                this.ctrl = re.data;
                console.error('返回', this.ctrl);

            });
        }
        // console.log('创建视频');
        // const id = this.getPara('videoId');
        // this.rewardedVideoAd = this.channelObj.createRewardedVideoAd({
        //     adUnitId: id
        // });

        // this.cacheVideo();

    }

    rewardedVideoAd
    videoFns


    showVideo(sucCall: any, para_endCall: any = null) {

        const id = this.getPara('videoId');
        if (!id || !this.channelObj) {
            return;
        }
        if (!this.rewardedVideoAd) {
            console.log('创建视频');
            this.rewardedVideoAd = mgtv.createRewardedVideoAd({
                adUnitId: id
            });
        }
        if (this.videoFns['onLoad']) {
            this.rewardedVideoAd.offLoad(this.videoFns['onLoad']);
        }
        if (this.videoFns['onError']) {
            this.rewardedVideoAd.offError(this.videoFns['onError']);
        }
        if (this.videoFns['onClose']) {
            this.rewardedVideoAd.offClose(this.videoFns['onClose']);
        }
        this.videoFns['onLoad'] = (err) => {
            console.log('video onload will show');
            this.rewardedVideoAd.show();
        };
        this.videoFns['onError'] = (err) => {
            console.error(err);
            //failCall && failCall();
            cc.game.resume();
            para_endCall && para_endCall();
        };
        this.videoFns['onClose'] = (res) => {
            if (res && res.isEnded) {
                console.log('下发奖励');
                sucCall && sucCall();
                return;
            }
            console.log('不发奖励res', res);
            //failCall && failCall();
            cc.game.resume();
            para_endCall && para_endCall();
        };
        this.rewardedVideoAd.onClose(this.videoFns['onClose']);
        this.rewardedVideoAd.onError(this.videoFns['onError']);
        this.rewardedVideoAd.onLoad(this.videoFns['onLoad']);

        console.log('video load start');
        this.rewardedVideoAd.load();

    }

}


